package com.example.findtherinbow.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.findtherinbow.Player;
import com.example.findtherinbow.R;

import java.util.List;

public class LeaderBoardAdapter extends RecyclerView.Adapter<LeaderBoardAdapter.ViewHolder> {

    private Context context;
    private List<Player> itemList;

    public LeaderBoardAdapter(Context context, List<Player> itemList) {
        this.context = context;
        this.itemList = itemList;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_leader_board, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Player item = itemList.get(position);

        // 设置TextView和ImageView的内容
        holder.textView1.setText(item.getPlayerName());
        holder.textView2.setText(String.valueOf(item.getPlayerScore()));

        if ("img_android_purple".equals(item.getPlayerAvatar())) {
            holder.imageView.setImageResource(R.drawable.img_android_purple);
        }

        if ("img_android_blue".equals(item.getPlayerAvatar())) {
            holder.imageView.setImageResource(R.drawable.img_android_blue);
        }

        if ("img_android_green".equals(item.getPlayerAvatar())) {
            holder.imageView.setImageResource(R.drawable.img_android_green);
        }

        if ("img_android_yellow".equals(item.getPlayerAvatar())) {
            holder.imageView.setImageResource(R.drawable.img_android_yellow);
        }

        if ("img_android_red".equals(item.getPlayerAvatar())) {
            holder.imageView.setImageResource(R.drawable.img_android_red);
        }
        ;

    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView1;
        TextView textView2;
        ImageView imageView;

        public ViewHolder(View itemView) {
            super(itemView);
            textView1 = itemView.findViewById(R.id.tv_leaderboard_name);
            textView2 = itemView.findViewById(R.id.tv_leaderboard_score);
            imageView = itemView.findViewById(R.id.iv_leader_board);
        }
    }
}
